package com.whut.report;

import com.alibaba.fastjson.JSONArray;
import com.whut.common.ReportCommonFunc;
import com.whut.model.ContractCheckTask;
import com.whut.model.ContractTaskProject;
import com.whut.model.Profiles;
import com.whut.parMapper.ParProblemCollectDetailMapper;
import com.whut.parMapper.ParProblemDescriptionMapper;
import com.whut.parMapper.ParSystemMapper;
import com.whut.parMapper.utils.MapperUtils;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.utils.FileUtil;
import com.whut.utils.MDoc;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.GroupMemberDetail;
import com.whut.v_model.ProblemCollectDetail;
import org.apache.commons.beanutils.BeanMap;

import javax.annotation.Resource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class PatrolReport {
    private ParSystemMapper parSystemMapper;
    private ParProblemDescriptionMapper parProblemDescriptionMapper;
    private ParProblemCollectDetailMapper parProblemCollectDetailMapper;
    private ReportServiceImpl reportServiceImpl;

    private MapperUtils mapperUtils;
    private Profiles profiles;
    private int checkId;
    private int projectId;
    private boolean isNew;
    public static final int WIDTH = 718;
    public static final int HEIGHT = 468;
    public PatrolReport(ParSystemMapper parSystemMapper,
                       ParProblemDescriptionMapper parProblemDescriptionMapper,
                       ParProblemCollectDetailMapper parProblemCollectDetailMapper,
                       ReportServiceImpl reportServiceImpl,
                       MapperUtils mapperUtils, Profiles profiles) {
        this.parSystemMapper = parSystemMapper;
        this.parProblemDescriptionMapper = parProblemDescriptionMapper;
        this.parProblemCollectDetailMapper = parProblemCollectDetailMapper;
        this.reportServiceImpl = reportServiceImpl;
        this.mapperUtils=mapperUtils;
        this. profiles =profiles;
    }


    public String patrolReport(int checkId, boolean isNew) throws UnsupportedEncodingException {
        HashMap<String, Object> dataMap = new HashMap<>();
        List<ContractTaskProject> header = mapperUtils.getContractTaskProjectMapper().getPatrolHeader(checkId);
        String projectName = header.get(0).getProjectName();
        String progress = header.get(0).getProgress();
        String jsUnit = header.get(0).getJsUnit();
        String sgUnit = header.get(0).getSgUnit();
        String jlUnit = header.get(0).getJlUnit();
        dataMap.put("projectName", projectName);
        dataMap.put("progress", progress);
        dataMap.put("jsUnit", jsUnit);
        dataMap.put("sgUnit", sgUnit);
        dataMap.put("jlUnit", jlUnit);

        List<ContractCheckTask> checkInfoList = mapperUtils.getContractchecktaskMapper().getCheckInfoByCheckId(checkId);
        String groupIdS = checkInfoList.get(0).getCheckGroupId();
        int groupId = Integer.parseInt(groupIdS);
        List<GroupMemberDetail> groupMemHead = mapperUtils.getGroupMemberDetailMapper().getGroupHead(groupId);
        List<GroupMemberDetail> groupMem = mapperUtils.getGroupMemberDetailMapper().getGroupMemb(groupId);
        String checkPeople = "";
        if (groupMemHead.size() == 0 && groupMem.size() == 0) {
            dataMap.put("checkPeople", " ");
        } else {
            for (GroupMemberDetail gmd : groupMemHead) {
                checkPeople += gmd.getUserName() + " ";
            }
            for (GroupMemberDetail gmd : groupMem) {
                checkPeople += gmd.getUserName() + " ";
            }
            dataMap.put("checkPeople", checkPeople);
        }


        List<ProblemCollectDetail> getCheckList = parProblemCollectDetailMapper.getPCDByCheckId(checkId);
        List<Object> sumList = new ArrayList<>();
        List<Object> checkList = new ArrayList<>();
        String pName4 = "";
        int temp = 0;
        for (ProblemCollectDetail pcd : getCheckList) {
            String picName = pcd.getPicName();
            JSONArray picNum = JSONArray.parseArray(picName);
            HashMap<String, Object> sumMap = new HashMap<>();
            if (pcd.getIsRemark().equals("N")) {
                sumMap.put("problemDes", pcd.getProblemDescription());
            } else {
                sumMap.put("problemDes", pcd.getRemark());
            }
            sumMap.put("suggestion", pcd.getSuggestion());
            if (picNum.size() == 0) {
                HashMap<String, Object> checkMap = new HashMap<>();
                checkMap.put("projectName", "检查项目：" + projectName);
                checkMap.put("checkPart", "检查部位：" + pcd.getCheckPart());
                if (pcd.getIsRemark().equals("N")) {
                    checkMap.put("problemDes", "检查问题：" + pcd.getProblemDescription());
                } else {
                    checkMap.put("problemDes", "检查问题：" + pcd.getRemark());
                }
                checkMap.put("suggestion", "建议措施：" + pcd.getSuggestion());
                List<Object> picList = new ArrayList<>();
                HashMap<String, Object> picMap = new HashMap<>();
//                picMap.put("pic", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png"));
                picMap.put("pic", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png"));
                picList.add(picMap);
                checkMap.put("picList", picList);
                checkList.add(checkMap);
            } else {
                HashMap<String, Object> checkMap = new HashMap<>();
                checkMap.put("projectName", "检查项目：" + projectName);
                checkMap.put("checkPart", "检查部位：" + pcd.getCheckPart());
                if (pcd.getIsRemark().equals("N")) {
                    checkMap.put("problemDes", "检查问题：" + pcd.getProblemDescription());
                } else {
                    checkMap.put("problemDes", "检查问题：" + pcd.getRemark());
                }
                checkMap.put("suggestion", "建议措施：" + pcd.getSuggestion());
                List<Object> picList = new ArrayList<>();
                for (int i = 0; i < picNum.size(); i++) {
                    String s="";
                    HashMap<String, Object> picMap = new HashMap<>();
                    if ("prod".equals(profiles.getActives())) {
                       s = MDoc.getImageStr(pcd.getPicPath() + picNum.get(i));
                    }
                    else{
                        s=MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                    }
                    picMap.put("pic", s); //服务器
//                    picMap.put("pic", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\12.png")); //测试端
                    picList.add(picMap);
                }
                checkMap.put("picList", picList);
                checkList.add(checkMap);
            }
            sumList.add(sumMap);
            temp++;
            if (temp != getCheckList.size()) {
                pName4 += pcd.getpName4() + "、";
            } else {
                pName4 += pcd.getpName4();
            }
        }
        dataMap.put("sumList", sumList);
        dataMap.put("checkList", checkList);
        dataMap.put("pName4", pName4);
        if (getCheckList.get(0).getRiskPoint().equals("提醒")) {
            dataMap.put("riskLevel", "隐患等级：4级");
        } else {
            String maxRiskPoint = pointsCalculate.getMaxRiskPoint(getCheckList);
            switch (maxRiskPoint) {
                case "1":
                    dataMap.put("riskLevel", "隐患等级：4级");
                case "2":
                    dataMap.put("riskLevel", "隐患等级：3级");
                case "3":
                    dataMap.put("riskLevel", "隐患等级：2级");
                case "4":
                    dataMap.put("riskLevel", "隐患等级：1级");
            }
        }
        List<ContractCheckTask> sign = mapperUtils.getContractchecktaskMapper().getBaseData(checkId);
        String checkTime = sign.get(0).getFinishTime();
        dataMap.put("checkTime", checkTime);

        if (sign.get(0).getSignPath() == null || sign.get(0).getSignPath().equals("")) {
            dataMap.put("jsUnitSign", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png"));
            dataMap.put("jlUnitSign", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png"));
            dataMap.put("sgUnitSign", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png"));
//            dataMap.put("jsUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\12.png"));
//            dataMap.put("jlUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\12.png"));
//            dataMap.put("sgUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\12.png"));
        } else {
            dataMap.put("jsUnitSign", MDoc.getImageStr(sign.get(0).getSignPath() + sign.get(0).getJsSign()));
            dataMap.put("jlUnitSign", MDoc.getImageStr(sign.get(0).getSignPath() + sign.get(0).getJlSign()));
            dataMap.put("sgUnitSign", MDoc.getImageStr(sign.get(0).getSignPath() + sign.get(0).getSgSign()));
//            dataMap.put("jsUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png"));
//            dataMap.put("jlUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png"));
//            dataMap.put("sgUnitSign", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\123.png"));
        }
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        dataMap.put("date", df.format(t));
        Date date1 = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileDate = d1.format(date1) + "patrolReport";
        MDoc mdoc = new MDoc();
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }

        DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
        String fileNameTime = dateTime.format(LocalDateTime.now());
        String reportName = header.get(0).getProjectName().replace('#', '号') + "巡查记录表" + fileNameTime;
        String ftlName = "patrolReport";


   //     BeanMap beanMap = new BeanMap(dataMap);

        return ReportCommonFunc.generateReport(dataMap, reportName, ftlName);
    }
}
